﻿using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Criterion;
using Web.Model;
using System.Linq;

namespace Web.DataAccess
{
	public class FinishedProjectsQuery
	{
        private User CurrentUser { get; set; }

        public FinishedProjectsQuery(User currentUser)
        {
            CurrentUser = currentUser;
        }

		public IEnumerable<Project> Load(ISession session)
		{
            var projects = session
                    .CreateCriteria<Project>()
                    .Add(Restrictions.Lt("Ending", DateTime.Now))
                    .AddOrder(Order.Desc("Beginning"))
                    .List<Project>().Where(project => project.IsCompleted);

			if (CurrentUser != null && CurrentUser.IsAdmin)
				return projects;
			return projects.Where(p => p.IsVisibleToUser(CurrentUser)).ToList();
		}
	}
}